
package com.example.testutil.util.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

//参考链接:http://blog.csdn.net/shulianghan/article/details/19028665
public class DbOperate {
    private static DbOperate mOperate;

    public DbManager manager;

    public SQLiteDatabase mDatabase;

    private DbOperate(Context context) {
        manager = DbManager.newinstance(context);
        mDatabase = manager.getdDatabase();
    }

    public static DbOperate getInstance(Context context) {
        if (mOperate == null) {
            mOperate = new DbOperate(context);
        }
        return mOperate;
    }

    public long insert(String table, String nullColumnHack, ContentValues values) {
        long rawId = mDatabase.insert(table, nullColumnHack, values);
        return rawId;
    }

    public int delete(String table, String whereClause, String[] whereArgs) {
        return mDatabase.delete(table, whereClause, whereArgs);
    }

    public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
        return mDatabase.update(table, values, whereClause, whereArgs);
    }

    public Cursor query(String sql, String[] selectionArgs) {
        // mDatabase.query(distinct, table, columns, selection, selectionArgs,
        // groupBy, having, orderBy, limit, cancellationSignal);
        return mDatabase.rawQuery(sql, selectionArgs);
    }

    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy,
            String having, String orderBy) {
        return mDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    }

    public void close() {
        mDatabase.close();
    }
}
